package com.hub.utils.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by hub on 2018/1/4.
 */
public class DateUtils {


    /**
     * 一小时
     */
    public static final int hour = 3600;

    public static final int day = 3600 * 24;

    /**
     * 一分钟 时间计算使用
     */
    public static final long minute=60 * 1000;

    /**
     * 获得当前日期
     *
     * @return
     */
    public static Date getNow() {
        Calendar cal = Calendar.getInstance();
        Date currDate = cal.getTime();
        return currDate;
    }

    public static String DateFormatToString(Date time) {
        SimpleDateFormat dateFormate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormate.format(time);
    }

    public static String DateFormatToString(Date time, String fmt) {
        SimpleDateFormat dateFormate=new SimpleDateFormat(fmt);
        return dateFormate.format(time);
    }

    public static Date DateFormatToDate(String time) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = sdf.parse(time);
        } catch (ParseException e) {

        }
        return date;
    }

    public static Date DateFormatToDate(String time, String fmt) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(fmt);
            date = sdf.parse(time);
        } catch (ParseException e) {

        }
        return date;
    }

    /**
     * 生成当前年月日字符串
     *
     * @return
     * @throws Exception
     */
    public static String getCurrentDateStr(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

}
